package com.alibaba.citrus.cr.sales.plan.facade.service.statistics.stat;

import com.alibaba.citrus.cr.common.util.SpringContextUtil;
import com.alibaba.citrus.cr.sales.plan.facade.service.statistics.stat.category.CategoryBusinessUnitStatService;
import com.alibaba.citrus.cr.sales.plan.facade.service.statistics.stat.category.CategoryChannelStatService;
import com.alibaba.citrus.cr.sales.plan.facade.service.statistics.stat.category.CategoryDepartmentStatService;
import com.alibaba.citrus.cr.sales.plan.facade.service.statistics.stat.category.CategoryEmployeeStatService;
import com.alibaba.citrus.cr.sales.plan.facade.service.statistics.stat.category.CategoryOrganizationStatService;
import com.alibaba.citrus.cr.sales.plan.facade.service.statistics.stat.model.SalesPlanStatRequest;
import com.alibaba.citrus.cr.sales.plan.facade.service.statistics.stat.organization.OrganizationBusinessUnitStatService;
import com.alibaba.citrus.cr.sales.plan.facade.service.statistics.stat.organization.OrganizationChannelStatService;
import com.alibaba.citrus.cr.sales.plan.facade.service.statistics.stat.organization.OrganizationDepartmentStatService;
import com.alibaba.citrus.cr.sales.plan.facade.service.statistics.stat.organization.OrganizationEmployeeStatService;
import com.alibaba.citrus.cr.sales.plan.facade.service.statistics.stat.organization.OrganizationStatService;
import com.alibaba.citrus.cr.sales.plan.facade.service.statistics.stat.scItem.ScItemStatService;
import com.alibaba.citrus.cr.sales.plan.facade.service.statistics.stat.category.*;
import com.alibaba.citrus.cr.sales.plan.facade.service.statistics.stat.organization.*;
import com.alibaba.citrus.cr.sales.plan.facade.service.statistics.stat.scItem.*;
import com.alibaba.cz.base.tool.log.Log;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.util.StopWatch;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * 货品维度汇总 结束后 组织维度和类目维度可并行开始处理
 * @author luyi
 * @date 2021/12/2 10:54 下午
 */
public class ConformityStatService implements Runnable {
    private static final Log log = Log.getLogger(ConformityStatService.class);

    private SalesPlanStatRequest statRequest;

    private CountDownLatch latch;

    private static final ThreadPoolTaskExecutor salesPlanTaskExecutor;
    static {
        salesPlanTaskExecutor = SpringContextUtil.getBean("salesPlanTaskExecutor",ThreadPoolTaskExecutor.class);
    }

    public ConformityStatService(SalesPlanStatRequest statRequest,CountDownLatch latch){
        this.statRequest = statRequest;
        this.latch = latch;
    }
    @Override
    public void run() {
        if (statRequest == null){
            return;
        }
        log.info("ConformityStatService_start_planId="+statRequest.getPlanId());
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        try {
            conformityCollect(statRequest);
        } catch (Exception e) {
            log.error("SalesPlanStatServiceImpl_SalesPlanStat_occar_exception,planId={},exception={}",statRequest.getPlanId(),e);
        }finally {
            if (latch != null){
                latch.countDown();
            }
        }
        stopWatch.stop();
        log.info("ConformityStatService_end_planId="+statRequest.getPlanId()+",elapsed_time="+stopWatch.getTotalTimeMillis()+"ms");
    }

    /**
     * 货品维度汇总
     */
    public void conformityCollect(SalesPlanStatRequest statRequest) throws InterruptedException {
        //货品维度汇总 结束后 组织维度和类目维度可并行开始处理
        ScItemStatService scItemStatService = new ScItemStatService(statRequest,null);
        scItemStatService.run();

//        CountDownLatch countDownLatch = new CountDownLatch(2);
//        salesPlanTaskExecutor.execute(new OrgStatService(statRequest,countDownLatch));
//        salesPlanTaskExecutor.execute(new CategoryStatService(statRequest,countDownLatch));
//        countDownLatch.await();

        CountDownLatch countDownLatch = new CountDownLatch(10);
        salesPlanTaskExecutor.execute(new OrganizationEmployeeStatService(statRequest,countDownLatch));
        salesPlanTaskExecutor.execute(new OrganizationDepartmentStatService(statRequest,countDownLatch));
        salesPlanTaskExecutor.execute(new OrganizationChannelStatService(statRequest,countDownLatch));
        salesPlanTaskExecutor.execute(new OrganizationStatService(statRequest,countDownLatch));
        salesPlanTaskExecutor.execute(new OrganizationBusinessUnitStatService(statRequest,countDownLatch));

        salesPlanTaskExecutor.execute(new CategoryEmployeeStatService(statRequest,countDownLatch));
        salesPlanTaskExecutor.execute(new CategoryDepartmentStatService(statRequest,countDownLatch));
        salesPlanTaskExecutor.execute(new CategoryChannelStatService(statRequest,countDownLatch));
        salesPlanTaskExecutor.execute(new CategoryOrganizationStatService(statRequest,countDownLatch));
        salesPlanTaskExecutor.execute(new CategoryBusinessUnitStatService(statRequest,countDownLatch));
        countDownLatch.await(30, TimeUnit.SECONDS);
    }
}
